{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "f60f464f6d624fc794caf719fc439163",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Output()"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import k3d\n",
    "import math\n",
    "import numpy as np\n",
    "\n",
    "# Y-Z flip\n",
    "model_matrix = [\n",
    "    1.0, 0.0, 0.0, 0.0,\n",
    "    0.0, -1.0, 0.0, 0.0,\n",
    "    0.0, 0.0, 1.0, 0.0,\n",
    "    0.0, 0.0, 0.0, 1.0\n",
    "]\n",
    "\n",
    "texture = k3d.texture(open('assets/texture.png', 'br').read(), 'png', \n",
    "                      rotation=[math.radians(90),1,0,0], \n",
    "                      model_matrix = model_matrix,\n",
    "                      name='Photo')\n",
    "\n",
    "plot = k3d.plot()\n",
    "plot += texture\n",
    "plot.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "texture.binary = open('assets/mandelbrot.jpg', 'br').read()\n",
    "texture.name = 'Fractal'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "texture.model_matrix = np.identity(4, dtype=np.float32)\n",
    "texture.transform.rotation = np.zeros_like(texture.transform.rotation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "texture.puv = np.array([\n",
    "    -5.0, 0.0, -5.0, # p\n",
    "    10.0, 0.0,  0.0, # u\n",
    "     0.0, 0.0, 10.0  # v\n",
    "], dtype=np.float32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.camera_reset()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from k3d.helpers import download\n",
    "filename = download('https://github.com/FNNDSC/data/raw/master/nifti/adi_brain/adi_brain.nii.gz')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "import nibabel as nib\n",
    "nii_source = nib.load(filename)\n",
    "img = nii_source.get_fdata()\n",
    "dx, dy, dz = nii_source.header.get_zooms()\n",
    "img = np.swapaxes(img,0,2)\n",
    "nz, ny, nx = img.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "41e79d28d31549098be6087073e481cb",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(IntRangeSlider(value=(0, 1024), description='Color range:', max=2012), Output()), _dom_c…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6d8b5118d5904d069701d8170e8bcf0c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(IntSlider(value=0, description='Slice:', max=55), Output()), _dom_classes=('widget-inter…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6626ba4aa2ce415b820c19a755da4f61",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Output()"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from ipywidgets import widgets,interact\n",
    "\n",
    "plot = k3d.plot()\n",
    "\n",
    "bounds = [0, nx * dx, 0, ny * dy, 0, nz * dz]\n",
    "\n",
    "texture = k3d.texture(attribute=img[0,:,:], \n",
    "                      color_map=k3d.basic_color_maps.Jet, \n",
    "                      color_range=[0.0, 1024.0],\n",
    "                      bounds=bounds,\n",
    "                      opacity_function=[-1,1,1,1],\n",
    "                      name='Slice')\n",
    "plot += texture\n",
    "\n",
    "@interact(x=widgets.IntRangeSlider(value=[0, 1024], min=0, max=np.max(img), step=1, description='Color range:'))\n",
    "def g(x):\n",
    "    texture.color_range = x\n",
    "    \n",
    "@interact(z=widgets.IntSlider(value=0,min=0,max=img.shape[0]-1,step=1, description='Slice:'))\n",
    "def g(z):\n",
    "    texture.attribute =img[z, :, :]\n",
    "    texture.transform.bounds = [bounds[0], bounds[1], bounds[2], bounds[3], -1 + z * dz, 1 + z * dz]\n",
    "\n",
    "plot.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = plot.get_binary_snapshot()\n",
    "\n",
    "with open('binary_snapshot.k3d', 'wb') as f:\n",
    "    f.write(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.grid_auto_fit = False\n",
    "plot.camera_auto_fit = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot += k3d.marching_cubes(img.astype(np.float32), level=900, bounds=bounds, color=0xff0000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bounds = [0, nx * dx, 0, ny * dy, 0, nz * dz]\n",
    "\n",
    "texture = k3d.texture(attribute=img[0,:,:], \n",
    "                      color_map=k3d.basic_color_maps.Jet, \n",
    "                      color_range=[0.0, 1024.0],\n",
    "                      opacity_function=[-1,1,1,1],\n",
    "                      bounds=bounds,\n",
    "                      name='Slice')\n",
    "plot += texture"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
